package com.colabo.j2ee.web.oaf.orgnization;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.colabo.j2ee.web.core.context.ContextUtil;
import com.colabo.j2ee.web.core.dictionary.CodeInfoCache;
import com.colabo.j2ee.web.core.dictionary.IPCodeInfoVO;
import com.colabo.j2ee.web.core.exception.ServiceException;
import com.colabo.j2ee.web.core.oldCache.CacheManager;
import com.colabo.j2ee.web.oaf.orgnization.model.SCUnitProfileVO;

public class UnitCache {
	private static UnitCache unitCache;

	private static CacheManager cache = new CacheManager();

	private final static String GROUP = "unit";

	private UnitCache() {
	}

	public synchronized static UnitCache getInstance() {
		if (unitCache == null) {
			unitCache = new UnitCache();
			initUnitCache();
		}

		return unitCache;
	}

	public synchronized void reloadUnit() {
		initUnitCache();
	}

	private static void initUnitCache() {
		IUnitService unitService = (IUnitService) ContextUtil.getBean(IUnitService.BEAN_ID);

		if (unitService == null) {
			return;
		}

		cache.flushGroup(GROUP);

		try {
			List unitList = unitService.getAllUnits();

			if (unitList == null) {
				return;
			}

			IPCodeInfoVO codeinfo = CodeInfoCache.newInstance().getCodeInfoVO("SystemConfiguration", "defaultUnitId");
			if (codeinfo != null) {
				String defaultUnitCode = codeinfo.getCodeinfoValue();
				String defaultUnitid = null;
				
				//根据设置的机构编码查找对应的机构编号
				HashMap map = new HashMap();
				map.put("ucode", defaultUnitCode);
				List list = unitService.queryUnit(map);
				if(list!=null && list.size()>0){
					SCUnitProfileVO vo = (SCUnitProfileVO)list.get(0);
					
					if(vo!=null){
						defaultUnitid = vo.getLogicalKey();
					}
				}
				
				if (defaultUnitid != null) {
					List defaultUnitList = unitService.getAllChildUnitByUnitId(defaultUnitid);
					cache.putGroup("defaultUnit", defaultUnitList, GROUP);
				}
			}
			for (int i = 0; i < unitList.size(); i++) {
				SCUnitProfileVO unitProfile = (SCUnitProfileVO) unitList.get(i);

				if (unitProfile == null) {
					continue;
				}

				
				if (unitProfile.getSupervisionUnit() == null || unitProfile.getSupervisionUnit().getLogicalKey() == null) {
					List list = (List) cache.getObj("unitchildRoot");
					if (list == null) {
						list = new ArrayList();
						list.add(unitProfile);
					} else {
						list.add(unitProfile);
					}
					cache.putGroup("unitchildRoot", list, GROUP);
					continue;
				}

				if (unitProfile.getTreePath() == null) {
					continue;
				}
				
				String superUnitId = unitProfile.getSupervisionUnit().getLogicalKey();
				if (superUnitId == null) {
					continue;
				}
				List list = (List) cache.getObj("unitchild" + superUnitId);
				if (list == null) {
					list = new ArrayList();
					list.add(unitProfile);

				} else {
					list.add(unitProfile);

				}
				cache.putGroup("unitchild" + superUnitId, list, GROUP);
				
				
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}

	public SCUnitProfileVO getUnitByUnitId(String unitId) throws ServiceException {
		IUnitService unitService = (IUnitService) ContextUtil.getBean(IUnitService.BEAN_ID);
		return unitService.getUnitByUnitId(unitId);
	}

	public List getDefaultChildUnits() throws ServiceException {
		Object obj = cache.getObj("defaultUnit");
		if(obj == null){
			return new ArrayList();
		}
		
		return (List) cache.getObj("defaultUnit");
	}

	public List getUnitsBySuperUnit(SCUnitProfileVO unit) throws ServiceException {
		List childUnitList = null;

		if (unit == null) {
			childUnitList = (List) cache.getObj("unitchildRoot");
		} else {
			childUnitList = (List) cache.getObj("unitchild" + unit.getLogicalKey());
		}
		if (childUnitList == null) {
			return new ArrayList();
		}
		return childUnitList;
	}
}
